package cn.chinaunicom.woplus.sample.web.spring;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.chinaunicom.woplus.openapi.spring.WoPlusClient;
import cn.chinaunicom.woplus.openapi.spring.WoPlusResponse;

@Controller
public class DemoController {

	private static final String platformID="501e9618-4305-4834-8847-d8044f8bad28";
	private static final String password ="{password}";
	
	@Autowired
	WoPlusClient woplusClient;
	
	@RequestMapping(value="/api/paymentcodesms",method=RequestMethod.GET,headers = {"content-type=application/json;charset=utf-8", "Accept=application/json"})
	public @ResponseBody WoPlusResponse paymentcodesms(HttpServletRequest request,@RequestParam("mobile")String mobile){
		
		WoPlusResponse resp = new WoPlusResponse();
		
		String api_url="https://open.wo.com.cn/openapi/rpc/paymentcodesms/v2.0";
		
		HashMap<String,Object> params = new HashMap<String,Object>();
		
		long num=new Random().nextLong();
		params.put("paymentUser", mobile);
		params.put("paymentType", 0);
		params.put("outTradeNo",Long.toString(num));
		params.put("paymentAcount", "001");
		params.put("subject", "金币一堆");
		params.put("totalFee", 0.01f);
		
		try {
			resp = woplusClient.post(api_url, params,false);
			if(resp.resultCode.equals("0")){
				request.getSession().setAttribute("paymentcodesms_param", params);
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resp;
	}
	
	@RequestMapping(value="/api/apppayment",method=RequestMethod.GET,headers = {"content-type=application/json;charset=utf-8", "Accept=application/json"})
	public @ResponseBody WoPlusResponse apppayment(HttpServletRequest request,@RequestParam("mobile")String mobile,@RequestParam("vcode")String vcode){
		
		WoPlusResponse resp = new WoPlusResponse();
		
		String api_url="https://open.wo.com.cn/openapi/rpc/apppayment/v2.0";
		
		@SuppressWarnings("unchecked")
		HashMap<String,Object> params =(HashMap<String,Object>) (request.getSession().getAttribute("paymentcodesms_param"));
		
		if(!params.get("paymentUser").equals(mobile))
			return resp;
		
		params.put("paymentcodesms",Long.decode(vcode).longValue());
		params.put("timeStamp", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		
		params.remove("paymentType");
		
		try {
			resp = woplusClient.post(api_url, params);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resp;
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////
	
	@RequestMapping(value="/api/sms",method=RequestMethod.POST,headers = {"content-type=application/json;charset=utf-8", "Accept=application/json"})
	public @ResponseBody WoPlusResponse sendSMS(){
		
		WoPlusResponse resp = new WoPlusResponse();
		
		String api_url="http://open.wo.com.cn/openapi/SMS/v1.0";
		
		HashMap<String,Object> params = new HashMap<String,Object>();
		HashMap<String,Object> msg = new HashMap<String,Object>();
		List<String> tos = new ArrayList<String>();
		tos.add("XXXX");
		tos.add("XXXX");
		msg.put("to", tos);
		msg.put("from", "1065547794003");
		msg.put("content", "hello");
		
		params.put("messages",msg);
		
		try {
			resp = woplusClient.post(api_url, params);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		return resp;
	}
	
	@RequestMapping(value="/api/getchannelpaymentsms",method=RequestMethod.POST,headers = {"content-type=application/json;charset=utf-8", "Accept=application/json"})
	public @ResponseBody WoPlusResponse getchannelpaymentsms(){
		
		WoPlusResponse resp = new WoPlusResponse();
		
		String api_url="https://open.wo.com.cn/openapi/getchannelpaymentsms/v1.0";
		
		HashMap<String,Object> params = new HashMap<String,Object>();
		
		long num=new Random().nextLong();
		params.put("outTradeNo",Long.toString(num));
		params.put("timeStamp", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
		params.put("subject", "金币一堆");
		params.put("totalFee", 0.05f);
		params.put("callbackUrl", "http://test.com:8080/notifycallback");
		params.put("appKey", "000000001");
		params.put("appName", "APP");
		
		try {
			resp = woplusClient.post(api_url, params, platformID, password);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		return resp;
	}
	
}
